package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealmapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /*分页查询套餐*/
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        List<SetmealVO> setmealList = setmealmapper.pageQuery(setmealPageQueryDTO);
        Page<SetmealVO> page = (Page<SetmealVO>) setmealList;

        return new PageResult(page.getTotal(), page.getResult());
    }

    /*新增套餐*/
    @Transactional
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealmapper.save(setmeal);

        //获取套餐的主键值
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        /*批量添加菜品和套餐的关联关系*/
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            setmealDishMapper.insertSetmealDishBatch(setmealDishes);
        }
    }

    /*套餐起售、停售*/
    @Override
    public void startOrStop(Integer status, Long id) {
        setmealmapper.startOrStop(status, id);
    }

    /*批量删除套餐*/
    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        /*判断套餐是否是起售状态再进行删除*/
        ids.forEach(id -> {
            Setmeal setmeal = setmealmapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        setmealmapper.deleteBatch(ids);
        setmealDishMapper.deleteBySetmealIdBatch(ids);
    }

    /*根据id查询套餐*/
    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = new SetmealVO();

        Setmeal setmeal = setmealmapper.getById(id);
        BeanUtils.copyProperties(setmeal, setmealVO);

        /*根据套餐id查询套餐菜品*/
        setmealVO.setSetmealDishes(setmealDishMapper.getBySetmealId(id));

        return setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealmapper.update(setmeal);

        /*清空套餐和菜品的关联关系 再次进行添加*/
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishMapper.deleteBySetmealIdBatch(Arrays.asList(setmeal.getId()));
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealDTO.getId());
            });
            setmealDishMapper.insertSetmealDishBatch(setmealDishes);
        }
    }

    /*根据分类id查询多个套餐*/
    @Override
    public List<Setmeal> getListCategoryId(Setmeal setmeal) {
        return setmealmapper.getListCategoryId(setmeal);
    }

    /*根据id查询套餐和对应的菜品*/
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemById(id);
    }
}
